package com.stox.core.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.stox.core.dao.mapper.SectorRowMapper;
import com.stox.core.model.Sector;
import com.stox.core.model.exception.StoxException;
import com.stox.design.DatabaseConfigurer;

public class SectorDao extends AbstractDao implements DatabaseConfigurer{

	private SectorRowMapper sectorRowMapper;
	
	@Override
	public int getOrder() {
		return 0;
	}
	
	@Override
	public Object invoke(List<String> payload) {
		
		if(!payload.contains("SECTOR")){
			try {
				getConnection().createStatement().execute(SectorRowMapper.createSql);
				getConnection().createStatement().execute(SectorRowMapper.createIndexSql);
			} catch (SQLException e) {
				throw new StoxException(e);
			}
		}
		
		if(!payload.contains("SECTOR_SCRIP")){
			try {
				getConnection().createStatement().execute(SectorRowMapper.createScripMappingSql);
			} catch (SQLException e) {
				throw new StoxException(e);
			}
		}
		return null;
	}
	
	public List<Sector> get(String exchangeCode) throws SQLException{
		PreparedStatement ps = getConnection().prepareCall(SectorRowMapper.selectSql+" where exchangeCode = ?");
		ps.setString(1, exchangeCode);
		return sectorRowMapper.map(ps.executeQuery());
	}

	public SectorRowMapper getSectorRowMapper() {
		return sectorRowMapper;
	}

	public void setSectorRowMapper(SectorRowMapper sectorRowMapper) {
		this.sectorRowMapper = sectorRowMapper;
	}
	
}
